set more off

use dataset, clear

local covariates20142010r pctwhite pct65plus pcths lmedianincome logvap2014 demvoteshare2012 registration2010 swing2012 Gub2010 Sen2010 Gub2010Sen2010 Gub2014 Sen2014 Gub2014Sen2014
local covariates20162012r pctwhite pct65plus pcths lmedianincome logvap2016 demvoteshare2012 registration2012 swing2012
local covariates20142010t pctwhite pct65plus pcths lmedianincome logvap2014 demvoteshare2012 turnout2010 swing2012 Gub2010 Sen2010 Gub2010Sen2010 Gub2014 Sen2014 Gub2014Sen2014
local covariates20162012t pctwhite pct65plus pcths lmedianincome logvap2016 demvoteshare2012 turnout2012 swing2012

local Z pcteligible2013
su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')
 
gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

foreach x in z Z Zz {
	gen `x'B = ""
	gen `x'CIL = ""
	gen `x'CIU = ""	
	gen `x'CI = ""
}
foreach x in z Z Zz {
	gen `x'N = ""
	gen `x'Ns = ""
	gen `x'bw = ""
	gen `x'cov = ""
	gen `x'rsq = ""
	gen `x'borderfe = ""
	gen `x'dim = ""
}

* bXdistance FE
tabulate border, gen(border_)
forvalues i = 1(1)34 {
	gen border_`i'Xx = border_`i'*x
	gen border_`i'Xzx = border_`i'*zx
}

gen index = _n

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest
program define myest
	args i bw cov borderfe dim
	mat bootresults = e(bootresults)
	svmat bootresults
	local j = 4
	foreach x in z Z Zz {
		replace `x'B = string(_b[`x'], "%20.02fc") if index == `i'
		_pctile bootresults`j' if bootresults3 == 0, nq(1000)
		replace `x'CIL = "[" + string(r(r25), "%20.02fc") + "," if index == `i'
		replace `x'CIU = string(r(r975), "%20.02fc") + "]" if index == `i'
		if `j' == 4 {
			replace `x'N = string(e(N), "%20.00fc") if index == `i'
			replace `x'Ns = string(e(N_fipsstate), "%20.00fc") if index == `i'
			replace `x'bw = "`bw'" if index == `i'
			replace `x'cov = "`cov'" if index == `i' 
			replace `x'rsq = string(e(r2), "%20.02fc") if index == `i'
			replace `x'borderfe = "`borderfe'" if index == `i'
			replace `x'dim = "`dim'" if index == `i'
		}
		local j = `j' + 1
	}	
	drop bootresults*
end

local nreps 500

*** registration 2014-2010 ***

xi: cgmwildboot dregistration20142010 z Z Zz x zx Zx Zzx i.border `covariates20142010r' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 1 "100 miles" "Yes" "Border" "1-D"
xi: cgmwildboot dregistration20142010 z Z Zz x zx Zx Zzx border_* `covariates20142010r' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 2 "100 miles" "Yes" "Border X distance" "1-D"
xi: cgmwildboot dregistration20142010 z Z Zz x zx Zx Zzx latit longit latXlon lat2 lon2 border_lon border_lat `covariates20142010r', cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 3 "100 miles" "Yes" "None" "2-D"

*** registration 2016-2012 ***

xi: cgmwildboot dregistration20162012 z Z Zz x zx Zx Zzx i.border `covariates20162012r' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 4 "100 miles" "Yes" "Border" "1-D"
xi: cgmwildboot dregistration20162012 z Z Zz x zx Zx Zzx border_* `covariates20162012r' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 5 "100 miles" "Yes" "Border X distance" "1-D"
xi: cgmwildboot dregistration20162012 z Z Zz x zx Zx Zzx latit longit latXlon lat2 lon2 border_lon border_lat `covariates20162012r', cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 6 "100 miles" "Yes" "None" "2-D"

* output

preserve

	keep if index < 7
	keep *B *CI* *N *Ns *bw *cov *borderfe *dim *rsq
	l

	sxpose, clear force
	drop if mi(_var1)
	l

	gen rowname = ""
	replace rowname = "Medicaid expansion" if _n == 1
	replace rowname = "High eligibility" if _n == 4
	replace rowname = "Expansion X eligibility" if _n == 7
	replace rowname = "Number of Counties" if _n == 10
	replace rowname = "Number of States" if _n == 11
	replace rowname = "Window" if _n == 12
	replace rowname = "Covariates" if _n == 13
	replace rowname = "R-squared" if _n == 14
	replace rowname = "Fixed effects" if _n == 15
	replace rowname = "Distance" if _n == 16
	order rowname 
	keep if _n <= 19

	#delimit;

	listtex 
		using "_output/tableA8.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{7}{c}}
			\toprule &
			\multicolumn{3}{c}{\underline{Registration}} & 
			\multicolumn{3}{c}{\underline{Registration}} \\
			& \multicolumn{3}{c}{\underline{2014-2010}} &
			\multicolumn{3}{c}{\underline{2016-2012}} \\
			&(1)&(2)&(3)&(4)&(5)&(6)\\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr

	l

restore
